package com.sunwayhorizo.health.platform.security.persistent;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.sunwayhorizo.health.platform.util.BeanHelper;

@Entity
@NamedQuery(name = "SecurityResource.findAll", query = "select sr from SecurityResource sr")
@TableGenerator(name = "SecurityResource_IdGenerator", table = "ET_Id_Generator", pkColumnName = "pkColumn", valueColumnName = "valueColumn", pkColumnValue = "SECURITY_RESOURCE_ID",allocationSize=1)
@Table(name = "SECURITY_RESOURCE", schema = "ETS")
public class SecurityResource implements java.io.Serializable {
	
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SecurityResource_IdGenerator")
	@Column(name = "RESOURCE_ID", unique = true, nullable = false, length = 32)
	private String resourceId;

	@Column(name = "RESOURCE_NAME", length = 30)
	private String resourceName;

	@Column(name = "RESOURCE_CODE", length = 20)
	private String resourceCode;
	
	@ManyToOne
	@JoinColumn(name = "FATHER_ID")
	private SecurityResource fatherResource;
	
	@Column(name = "CREATOR_NAME", length = 20)
	private String creatorName;
	
	@Column(name = "RESOURCE_SRC", length = 200)
	private String resourceSrc;
	
	@Temporal(TemporalType.DATE)
	@Column(name = "CREATE_TIME", length = 7)
	private Date createTime;
	
	@Column(name = "STATE", length = 1)
	private String state;
	
	@Column(name = "MODIFIER_NAME", length = 20)
	private String modifierName;
	
	@Temporal(TemporalType.DATE)
	@Column(name = "MODIFY_TIME", length = 7)
	private Date modifyTime;
	
	@Column(name = "REF_ID", length = 32)
	private String refId;
	
	@Column(name = "REF_TYPE", length = 32)
	private String refType;
	
	@Column(name = "REMARK", length = 200)
	private String remark;
	
	@Column(name = "ICON_REF", length = 200)
	private String iconRef;

	
	@OneToMany(mappedBy = "fatherResource",fetch = FetchType.LAZY)
	private List<SecurityResource> dbChildren = new ArrayList<SecurityResource>(0);
	
	@Transient
	private List<SecurityResource> children = null;
	
	@OneToMany(mappedBy = "resource",fetch = FetchType.LAZY)
	private List<SecurityResourceDataType> resourceDataTypes = new ArrayList<SecurityResourceDataType>(
			0);
	
	@OneToMany(mappedBy = "resource",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
	private List<SecurityPermission> permissions = new ArrayList<SecurityPermission>(
			0);

	public SecurityResource() {
	}

	public SecurityResource(String resourceId) {
		this.resourceId = resourceId;
	}

	public SecurityResource(String resourceId, String resourceName,
			String resourceCode, SecurityResource fatherResource,
			String creatorName, String resourceSrc, Date createTime,
			String state, String modifierName, Date modifyTime, String refId,
			String refType, String remark, String iconRef,
			List<SecurityResource> dbChildren,
			List<SecurityResourceDataType> resourceDataTypes,
			List<SecurityPermission> permissionList) {
		super();
		this.resourceId = resourceId;
		this.resourceName = resourceName;
		this.resourceCode = resourceCode;
		this.fatherResource = fatherResource;
		this.creatorName = creatorName;
		this.resourceSrc = resourceSrc;
		this.createTime = createTime;
		this.state = state;
		this.modifierName = modifierName;
		this.modifyTime = modifyTime;
		this.refId = refId;
		this.refType = refType;
		this.remark = remark;
		this.iconRef = iconRef;
		this.dbChildren = dbChildren;
		this.resourceDataTypes = resourceDataTypes;
		this.permissions = permissionList;
	}

	public String getResourceId() {
		return this.resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public String getResourceName() {
		return this.resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public String getResourceCode() {
		return this.resourceCode;
	}

	public void setResourceCode(String resourceCode) {
		this.resourceCode = resourceCode;
	}

	public SecurityResource getFatherResource() {
		return fatherResource;
	}

	public void setFatherResource(SecurityResource fatherResource) {
		this.fatherResource = fatherResource;
	}

	public String getCreatorName() {
		return this.creatorName;
	}
	
	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}
	
	public String getResourceSrc() {
		return this.resourceSrc;
	}

	public void setResourceSrc(String resourceSrc) {
		this.resourceSrc = resourceSrc;
	}

	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getModifierName() {
		return this.modifierName;
	}

	public void setModifierName(String modifierName) {
		this.modifierName = modifierName;
	}

	public Date getModifyTime() {
		return this.modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	public String getRefId() {
		return this.refId;
	}

	public void setRefId(String refId) {
		this.refId = refId;
	}

	public String getRefType() {
		return refType;
	}

	public void setRefType(String refType) {
		this.refType = refType;
	}

	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public List<SecurityResource> getChildren() {
		return children;
	}

	public void setChildren(List<SecurityResource> children) {
		this.children = children;
	}

	public List<SecurityResourceDataType> getResourceDataTypes() {
		return this.resourceDataTypes;
	}

	public void setResourceDataTypes(
			List<SecurityResourceDataType> resourceDataTypes) {
		this.resourceDataTypes = resourceDataTypes;
	}

	public List<SecurityPermission> getPermissions() {
		return this.permissions;
	}

	public void setPermissions(
			List<SecurityPermission> permissions) {
		this.permissions = permissions;
	}
	
	public SecurityPermission addPermissions(SecurityPermission securityPermission) {
	    permissions.add(securityPermission);
		securityPermission.setResource(this);
		return securityPermission;
	}

	public SecurityPermission removePermissions(SecurityPermission securityPermission) {
	    permissions.remove(securityPermission);
		securityPermission.setResource(null);
		return securityPermission;
	}	

	public List<SecurityResource> getDbChildren() {
		return dbChildren;
	}

	public void setDbChildren(List<SecurityResource> dbChildren) {
		this.dbChildren = dbChildren;
	}
	
	public void addChild(SecurityResource r){
		if( this.getChildren() == null ){
			this.setChildren(new ArrayList<SecurityResource>());
		}
		this.getChildren().add(r);
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getIconRef() {
		return iconRef;
	}

	public void setIconRef(String iconRef) {
		this.iconRef = iconRef;
	}
	
	@Override
	public String toString() {
		return BeanHelper.toString(this);
	}
}
